package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/SelectByBrandServlet")
public class SelectByBrandServlet extends HttpServlet {
    //搜索

    private final BrandService brandService = new BrandService();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得传入数据
        String line = request.getReader().readLine();
        System.out.println(line);
        Brand brand = JSON.parseObject(line, Brand.class);
        System.out.println(brand);
        List<Brand> brands = brandService.selectByBrand(brand);
        //封装
        String jsonString = JSON.toJSONString(brands);
        //3.将数据打印回浏览器 (返回的是json格式)
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(jsonString);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
